package com.cxm.qyyz.ui;

import android.Manifest;
import android.content.Intent;
import android.os.Build;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.cxm.qyyz.BuildConfig;
import com.cxm.qyyz.R;
import com.cxm.qyyz.SPManager;
import com.cxm.qyyz.app.App;
import com.cxm.qyyz.app.Navigator;
import com.cxm.qyyz.base.activity.BaseActivity;
import com.cxm.qyyz.contract.MainContract;
import com.cxm.qyyz.presenter.MainPresenter;
import com.cxm.qyyz.utils.BadgeUtils;
import com.cxm.qyyz.widget.plus.StatusBarPlus;

import java.util.Arrays;
import java.util.List;

import pub.devrel.easypermissions.EasyPermissions;

public class SplashActivity extends BaseActivity<MainPresenter> implements MainContract.View {

    private final String[] permissions = {
            Manifest.permission.READ_PHONE_STATE,
    };

    private final int CODE_APP_SETTING = 2;
    private final String LAST_TIME = "lastTime";


    /**
     * 网络是否请求完成
     */
    int isOk = 0;


    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void initBeforeBindLayout() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
//        SPUtils.getInstance().put(Constants.LOGIN_FIRST, true, true);
//        if (!isTaskRoot()) {
//            Intent mainIntent = getIntent();
//            String action = mainIntent.getAction();
//            if (mainIntent.hasCategory(Intent.CATEGORY_LAUNCHER) && action.equals(Intent.ACTION_MAIN)) {
//                finish();
//                return;
//            }
//        }
//        String umInit = SPUtils.getInstance().getString("umInit");
//        if (!"1".equals(umInit)) {
//            AdvanceDialog.getInstance()
//                    .setOnAdvanceListener(new AdvanceDialog.OnAdvanceListener() {
//                        @RequiresApi(api = Build.VERSION_CODES.M)
//                        @Override
//                        public void onAccept() {
//                            SPUtils.getInstance().put(Constants.ADVANCE_INIT, "1", true);
//                            checkUserPermissions();
//                        }
//
//                        @Override
//                        public void onReject() {
//                            AppUtil.getInstance().exitApp();
//                        }
//                    })
//                    .show(getSupportFragmentManager(), "AdvanceDialog");
//        } else {
//            checkUserPermissions();
//        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    protected void initEvents() {
        if (BuildConfig.DEBUG){
            App.getInstance().initAppComponent();
        }
        StatusBarPlus.setTransparent(this);
        SPManager.setHuaWeiTime();
//        SophixManager.getInstance().queryAndLoadNewPatch();
        SPManager.setFirstOpenApp(true);
        SPManager.setFirstDownMusic(true);
//        mPresenter.getSplashUrl(true);
        BadgeUtils.setCount(0,this);
    }

    @Override
    protected void initInjector() {
        mActivityComponent.inject(this);
    }

    @Override
    protected void setScreenOrientation() {

    }

    @Override
    protected void initStatusBar() {
    }

    @Override
    protected boolean supportStateController() {
        return false;
    }


    boolean isPermissions = false;

    @Override
    public void onPermissionsGranted(int requestCode, @androidx.annotation.NonNull List<String> perms) {
        List<String> data = Arrays.asList(permissions);
        if (perms.containsAll(data)) {
            startTimer();
        }
    }

    @Override
    public void onPermissionsDenied(int requestCode, @androidx.annotation.NonNull List<String> perms) {
        if (EasyPermissions.somePermissionDenied(this, permissions) || EasyPermissions.somePermissionPermanentlyDenied(this, Arrays.asList(permissions))) {
//            if ("vivo".equalsIgnoreCase(BuildConfig.channelName)) {
                long currentTimeMillis = System.currentTimeMillis();
//            }
            startTimer();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == CODE_APP_SETTING) {
            if (EasyPermissions.hasPermissions(this, permissions)) {
            }
            startTimer();
        }
    }

    @Override
    public void onRationaleAccepted(int requestCode) {

    }

    @Override
    public void onRationaleDenied(int requestCode) {

    }

    private void startTimer() {
        isPermissions = true;
        jumpToMain();
    }

    private void jumpToMain() {
        if (isPermissions) {
            if (isOk == 1) {
                App.getInstance().initSDK();
                Navigator.openMain(this);
                finish();
            } else if (isOk == 2) {
            }
        }
    }

    @Override
    public void onBackPressed() {

    }
}
